﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Motoball.Paddles
{
    public class Roland : PaddleContent
    {
        public Roland(Paddle paddle)
            : base(paddle)
        {
            animation = new RolandAnimation();
            paddleHeight = 244;
            paddleWidth = 516;
            maxPaddleDistance = 1000;//440;
            ballHeight = 2310;
            type = "roland";
        }

        public override void Initialize(float innerRadius)
        {
            shield = new BoneProperties[1];
            shield[0] = new BoneProperties(paddle.basicModel.model.Bones["Schild"]);
            base.Initialize(innerRadius);
        }

        public override void SpecialMovement(bool active)
        {
            
        }
    }

    public class RolandAnimation : PaddleAnimation
    {
        BoneProperties exhaustLeft;
        BoneProperties exhaustRight;
        

        double[] timer;
        public RolandAnimation()
            : base()
        {
            timer = new double[2];
            timer[0] = 0.01f;
            timer[1] = 0.03f;
            //randomPos = new Vector3[2];
            //randomPos[0] = Vector3.Zero;
            //randomPos[1] = Vector3.Zero;
        }


        public override void Initialize(float innerRadius, Model model)
        {
            #if !XBOX
            save.fileName = "roland";
#endif
            base.Initialize(innerRadius, model);
            leftWheel = model.Bones["Rad1_Felge"];
            rightWheel = model.Bones["Rad2_Felge"];

            leftWheelTransform = leftWheel.Transform;
            rightWheelTransform = rightWheel.Transform;

            exhaustLeft = new BoneProperties(model.Bones["Auspuff1"]);
            exhaustRight = new BoneProperties(model.Bones["Auspuff2"]);

            nextPos = new Vector3[2];
            lastPos = new Vector3[2];
            posIndex = new int[2];
            posIndex[0] = 1;
            posIndex[1] = 4;
            nextPos[0] = exhaustKeyFrames[posIndex[0]] /5f;
            nextPos[1] = exhaustKeyFrames[posIndex[1]] / 5f;
            lastPos[0] = exhaustKeyFrames[posIndex[0] - 1] / 5f;
            lastPos[1] = exhaustKeyFrames[posIndex[1] - 1] / 5f;
        }

        public override void UpdateColor(Vector3 color, BasicModel basicModel)
        {
            //basicModel.diffColors["Auspuff1"] = Vector3.Lerp(Vector3.One, color, 0.6f);
            //basicModel.diffColors["Auspuff2"] = Vector3.Lerp(Vector3.One, color, 0.6f);

            basicModel.diffColors["Schild"] = Vector3.Lerp(Vector3.One, color, 0.7f);

            //basicModel.diffColors["Roland_Chassis"] = Vector3.Lerp(Vector3.One, color, 0.6f);

            //back-side gas-pipes
            //basicModel.diffColors["Obj_000003"] = Vector3.Lerp(Vector3.Zero, color, 0.5f);
            //basicModel.diffColors["Obj_000005"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);

            ////front-side gas-pipes
            //basicModel.diffColors["Obj_000004"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);
            //basicModel.diffColors["Obj_000006"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);

        }
        
        
        Vector3[] nextPos;
        Vector3[] lastPos;
        int[] posIndex;
        public override void UpdateAnimation(GameTime gameTime)
        {
            for (int i = 0; i < 2; i++)
            {
                timer[i] += gameTime.ElapsedGameTime.TotalSeconds;
                //if defined time has passed - create a new random-direction exhaust is shaking towards
                if (timer[i] > 0.07f)
                {
                    timer[i] -= 0.07f;
                    posIndex[i] = (posIndex[i] + 1) % 10;
                    lastPos[i] = nextPos[i];
                    nextPos[i] = exhaustKeyFrames[posIndex[i]] / 5f;
                   
                    //randomPos[i] = new Vector3(
                    //    (float)random.NextDouble(),
                    //    (float)random.NextDouble(),
                    //    (float)random.NextDouble()) * 3;
                }
            }
            exhaustLeft.modelBone.Transform = Matrix.CreateTranslation(
                Vector3.Lerp(lastPos[0], 
                    nextPos[0], 
                    (float)Math.Sin(timer[0])))
                * exhaustLeft.sourceTransform;
            exhaustRight.modelBone.Transform = Matrix.CreateTranslation(
                Vector3.Lerp(lastPos[1], 
                nextPos[1], 
                (float)Math.Sin(timer[1])))
                * exhaustRight.sourceTransform;
            base.UpdateAnimation(gameTime);
        }
    }
}
